package org.adl.registry.website;

import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.adl.registry.types.Registry;
import org.adl.registry.website.rim.RimProxy;

/**
 *
 * @author Jacob Marks - Joint ADL Co-Lab
 * @version 1.0
 * @license This software is licensed as open source software pursuant to the terms defined in the Joint ADL Co-Lab Open Source Software License.  Use of any part of this software constitutes acceptance of this license. The Joint ADL Co-Lab open source agreement is available at {@link http://adlregistry.adlnet.gov/source_license.html}.
 */
public abstract class SubmissionRequestHandler extends RequestHandler
{
    private String mCommand;
    private String mUserId;
    private String mPassword;
    private String mTargetIdentifier;
    private String mResponseType;
    
    /** Creates a new instance of SubmissionRequest */
    public SubmissionRequestHandler()
    {
    }
    
    public String sendSubmissionRequest()
    throws IOException
    {
        String rimResponse;
        
        ServletContext context = super.getServletContext();
        
        RimProxy p = new RimProxy(super.getRimUrl());
        p.openConnection();
        
        p.writeParam("command", this.mCommand);
        
        p.writeParam("cak", super.getClientAccessKey());
        p.writeParam("registry", super.getRegistry());
        
        if ((this.mResponseType != null) && (this.mResponseType.length() > 0))
        {
            p.writeParam("rtype", this.mResponseType);
        }
        
        p.writeParam("password", this.mPassword);
        p.writeParam("userid", this.mUserId);
        
        rimResponse = p.writeResponseTo();
        
        p.close();
        
        return rimResponse;
    }
    
     public String getCommand()
    {
        return this.mCommand;
    }

    public void setCommand(String iCommand)
    throws InvalidRequestException
    {
        if ((iCommand == null) || (iCommand.trim().length() == 0))
        {
            throw new InvalidRequestException(".");
        }
        
        iCommand = iCommand.trim();
        
        this.mCommand = iCommand;
    }

    public String getUserId()
    {
        return this.mUserId;
    }

    public void setUserId(String iUserId)
    throws InvalidRequestException
    {
        if ((iUserId == null) || (iUserId.trim().length() == 0))
        {
            throw new InvalidRequestException(".");
        }
        
        iUserId = iUserId.trim();
        
        this.mUserId = iUserId;
    }

    public String getPassword()
    {
        return this.mPassword;
    }

    public void setPassword(String iPassword)
    throws InvalidRequestException
    {
        if ((iPassword == null) || (iPassword.trim().length() == 0))
        {
            throw new InvalidRequestException(".");
        }
        
        iPassword = iPassword.trim();
        
        this.mPassword = iPassword;
    }

    public String getTargetIdentifier()
    {
        return this.mTargetIdentifier;
    }

    public void setTargetIdentifier(String iTargetIdentifier)
    throws InvalidRequestException
    {
        if ((iTargetIdentifier == null) || (iTargetIdentifier.trim().length() == 0))
        {
            throw new InvalidRequestException(".");
        }
        
        iTargetIdentifier = iTargetIdentifier.trim();
        
        this.mTargetIdentifier = iTargetIdentifier;
    }

    public String getResponseType()
    {
        return this.mResponseType;
    }

    public void setResponseType(String iResponseType)
    throws InvalidRequestException
    {
        if ((iResponseType == null) || (iResponseType.trim().length() == 0))
        {
            throw new InvalidRequestException(".");
        }
        
        iResponseType = iResponseType.trim();
        
        this.mResponseType = iResponseType;
    }
    
    protected String ProcessMultipart()
    throws IOException
    {
        String rimResponse;
        
        HttpServletRequest request = super.getServletRequest();
        ServletContext context = super.getServletContext();
        
        //TODO: Parse the multipart request to determine what registry to send request to.
        String rimUrl = PortalUtil.getRimUrl(context, Registry.OPERATIONAL_REGISTRY_HANDLE);
        
        RimProxy proxy = new RimProxy(rimUrl);
        
        proxy.openConnection();
        
        proxy.forwardMultipart(request.getInputStream());
        
        rimResponse = proxy.writeResponseTo();
        
        proxy.close();
        return rimResponse;
    }
}
